import Vue from 'vue'
import VueRouter from 'vue-router'
// import Detail from '@/views/Detail'
import Layout from '@/views/Layout'
// import Like from '@/views/Like'
// import User from '@/views/User'
// import Article from '@/views/Article'
// import Collect from '@/views/Collect'
// import Login from '@/views/Login'
// import Register from '@/views/Register'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/detail/:id',
    component: () => import('@/views/Detail')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: 'article',
        component: () => import('@/views/Article')
      },
      {
        path: 'collect',
        component: () => import('@/views/Collect')
      },
      {
        path: 'like',
        component: () => import('@/views/Like')
      },
      {
        path: 'user',
        component: () => import('@/views/User')
      }

    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '/register',
    component: () => import('@/views/Register')
  },
  {
    path: '/sou',
    component: () => import('@/views/Sou')
  }

]

const router = new VueRouter({
  routes
})
const WRITE_LIST = ['/login', '/register']
router.beforeEach((to, from, next) => {
  if (getToken()) {
    next()
  } else {
    if (WRITE_LIST) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
